<?php
namespace User\Controller;
use Think\Controller;
class IndexController extends Controller {

    public function index(){
        $user = session('user');
        if(empty($user)){redirect('Home/Index/loginView');}
            $menu=M('menu','','DB_CONFIG1');
            //查询一级菜单
            $menu_list= $menu
                ->join('interaction ON interaction.inter_mid = menu.menu_id')
                ->where('(menu_parent=0) AND (interaction.inter_pid='.$user['user_role'].')AND (menu.switch=1)')
                ->order('inter_mid asc')
                ->select();
				
            //查询二级菜单
            $second_menu= $menu
                ->join('interaction ON interaction.inter_mid = menu.menu_id')
                ->where('(menu_parent <> 0) AND (interaction.inter_pid='.$user['user_role'].') AND (menu.switch=1)')
                ->order('inter_mid asc')
                ->select();
				
			if($user['position']==1){
				foreach($menu_list as $k => $v){
					if(($v['menu_id']==57) || ($v['menu_id']==51)){
						unset($menu_list[$k]);
					}
				}
				foreach($second_menu as $ke => $va){
					if(($v['menu_id']==57) || ($v['menu_id']==51)){
						unset($second_menu[$k]);
					}
				}
			}
        $logo=M('logo')->field('id,logo,ico,crm_logo,web_name,web_name_en')->find();
        $this->assign('logo',$logo);
        $this->assign('user',$user);
        $this->assign('menu_list',$menu_list);
        $this->assign('second_menu',$second_menu);

        $this->display();
    }

    public function common_css(){
        $logo=M('logo')->field('id,logo,ico,crm_logo')->find();
        $this->assign('logo',$logo);
        $this->display();
    }

    /* $request = "action=getaccountbalance&login=".$user['user_id']."\0";  //余额
         *$reoups = mt4_operation($request);
          parse_str($reoups,$size);
          $balance = $size['balance']; //余额*/
    //首页显示页
    public function index_frame(){
        $user =session('user');
        if(empty($user)){redirect('Index/login', 1, '身份信息过期，正在跳转登录页面...');}
        $fo =mktime(0,0,0,date('m'),date('d'),date('Y'));           //当天开始时间
        $to =mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;   //当天结束时间
        $month_fo  =mktime(0,0,0,date('m'),1,date('Y'));               //本月开始时间
        $month_to =mktime(23,59,59,date('m'),date('t'),date('Y'));  //本月结束时间
//        if($user['type']==1){
//            $mt4_users = M('mt4_users', '', 'DB_CONFIG3');//MT用户数据库
//            $mt4_trades = M('mt4_trades', '', 'DB_CONFIG3');//MT交易数据库
//        }else{
//            $mt4_users = M('mt4_users', '', 'DB_CONFIG2');//MT用户数据库
//            $mt4_trades = M('mt4_trades', '', 'DB_CONFIG2');//MT交易数据库
//        }
        $mt4_users = M('mt4_users', '', 'DB_CONFIG2');//MT用户数据库
        $mt4_trades = M('mt4_trades', '', 'DB_CONFIG2');//MT交易数据库

        $where2['LOGIN']   = $user['user_id'];
        $balance = $mt4_users->where($where2)->field('BALANCE')->find();//余额
        //查询未到期的航线币
        /*$firstday = date("Y-m-d");
        $last_month=strtotime($firstday." -1 month")+1;
        $currentday = date("Y-m-0");
        $current_month=strtotime($currentday);
        $airline=M('into_gold')->where('into_time >'.$last_month)->sum('final_money');//未到期总和
        $airline_current=M('into_gold')->where('into_time >'.$current_month)->sum('final_money');//本月总和
        $rate1=M('into_gold_set')->field('rate')->where(['type'=>1,'state'=>1])->find();//本月
        $rate2=M('into_gold_set')->field('rate')->where(['type'=>2,'state'=>1])->find();//上月
        if($balance['balance']>$airline){
            $balance['balanceAir']=($balance['balance']-($airline-$airline_current))*$rate1['rate']+($airline-$airline_current)*$rate2['rate'];
        }else if($balance['balance']>$airline_current){
            $balance['balanceAir']=($balance['balance']-$airline_current)*$rate2['rate']+($airline_current*$rate1['rate']);
        }else{
            $balance['balanceAir']=$balance['balance']*$rate1['rate'];
        }*/

        $rate=M('into_gold_set')->field('rate')->where(['type'=>1,'state'=>1])->find();//本月
        $balance['balanceAir1']=($balance['balance']*($rate['rate']*0.0115+$rate['rate']))-($balance['balance']*$rate['rate']);
        $balance['balanceAir2']=($balance['balance']*($rate['rate']*0.0325+$rate['rate']))-($balance['balance']*$rate['rate']);

        $where['into_time'] = array('between',array($fo,$to));
        $where['mt4']=$user['user_id'];
        $oneday = M('into_gold')->where($where)->sum('final_money');//今日入金

        $fo = date('Y-m-d',$fo);
        $to = date('Y-m-d H:i:s',$to);
        $month_fo = date('Y-m-d H:i:s',$month_fo);
        $month_to = date('Y-m-d H:i:s',$month_to);

        $where2['CLOSE_TIME'] = array('between',array($fo,$to));
        $where2['CMD'] = array('lt',2);
        $where3['LOGIN']   = $user['user_id'];
        $where3['CLOSE_TIME'] = array('between',array($month_fo,$month_to));
        $where3['CMD'] = array('lt',2);
        $volume =  $mt4_trades->where($where2)->sum('VOLUME');  //我的今日交易量
        $month_volume =  $mt4_trades->where($where3)->sum('VOLUME');//我的本月交易量

        $list = M('user')->field('user_id,user_recommend')->where('user_recommend >0')->select(); //,name_zh_cn,user_email,position,level
        $childrenFile='Uploads/getChilds/'.date('Ymd').'-'.$user['user_id'].'.txt';
        if(file_exists($childrenFile)){//判断更新缓存
            $children=file_get_contents($childrenFile);
            $mt4_list=unserialize($children);
        }else{
            set_time_limit(120);//程序执行时间
            $mt4_list = $this->getChilds($list,$user['user_id'],[],0);
            $childrenData=serialize($mt4_list);
            file_put_contents($childrenFile,$childrenData);
        }
        $number = count($mt4_list);//我的团队人数

        $section=C('section');//获取号段
        $where4['LOGIN']=array(array('in',$mt4_list ),array('between',array($section['min'],$section['max'])));
        $where4['CMD'] = array('lt',2);
        $where4['CLOSE_TIME'] = array('between',array($fo,$to));
        $sum_num = $mt4_trades->field('sum(VOLUME) as volume')->where($where4)->select();//今日团队交易量
        $count = M('article')->where(['type'=>2])->count();//
        $Page = new \Think\Page($count,5);
        $notice = M('article')->where(['type'=>2])->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
        foreach ($notice as $k=>$v){
            $notice[$k]['add_time'] = timeFormat($v['add_time']);
        }
        $where5='(CMD = 6) and (LOGIN ='.$user['user_id'].')  and (COMMENT like \'CommBo%\')';
        $one_commission = $mt4_trades->where( $where5.'and (CLOSE_TIME between \''.$fo.'\' and \''.$to.'\')')->sum('PROFIT');//佣金当日收入
        $month_commission = $mt4_trades->where($where5.'and (CLOSE_TIME between \''.$month_fo.'\' and \''.$month_to.'\')')->sum('PROFIT');//佣金月收入

        $show = $Page->show();// 分页显示输出
        $this->assign('page',$show);
		$this->assign('user',$user);
        $this->assign('one_commiss',round($one_commission,2));
        $this->assign('month_commiss',round($month_commission,2));
        $this->assign('notice',$notice);
        $this->assign('balance',$balance);
        $this->assign('oneday',round($oneday,2));
        $this->assign('volume',round($volume/100,2));
        $this->assign('month_volume',round($month_volume/100,2));
        $this->assign('number',$number);
        $this->assign('sum_num',round($sum_num[0]['volume']/100,2));
        $this->display();
    }

    //公告详情
    public function notice_OneData(){
        $id= I('id')?I('id'):die('没有信息,请重试');
        $data= M('article')->where(array('id'=>$id))->find();
        if(!empty($data)){
            $data['content'] = htmlspecialchars_decode( $data['content']);
            $this->assign('data',$data);
            $this->display();
        }
    }


    //获所有下级，只有账号
    public function getChilds($data,$uid,$arr){
        foreach ($data as $k => $v){
            if($uid==$v['user_recommend']){
                unset($data[$k]);
                $arr[]=$v['user_id'];
                $arr=$this->getChilds($data,$v['user_id'],$arr);
            }
        }
        return $arr;
    }

    //退出登录
    public function loginOut(){
        // 清楚所有session
        session(null);
        $this->redirect('Home/Index/loginView', '正在退出登录...');
    }





}